// 导入组件
import Vue from 'vue';
import Router from 'vue-router';

// 导入工具类
import { getStore } from "@/utils/util";
import { TOKEN } from '@/utils/const';

// ==================================== 公有页面 ==================================== 
// 登录
const Login = () => import('@/views/public/Login');
// 侧边栏菜单 + 顶部菜单
const Layout = () => import('@/views/public/Layout');
// 404
const NotFound = () => import('@/views/public/Error404');
// TODO: 测试页面
const Test = () => import('@/views/public/Test');

// ==================================== 课程备课 ==================================== 
// 课程
const ManageCourse = () => import('@/views/teacher/manageCourse/ManageCourse');
// 课件
const ManageCourseware = () => import('@/views/teacher/manageCourse/ManageCourseware');
// 课件-卡片列表页面
const CoursewareCardList = () => import('@/views/teacher/manageCourse/childComponents/CoursewareCardList');
// 课件-新增课件
const AddCourseware = () => import('@cpns/content/teacher/AddCourseware');

// 启用路由 『路由注册』
Vue.use(Router);

// 定义路由列表
const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    desc: "登录",
    path: '/login',
    name: 'login',
    component: Login,
    meta: {
      requireAuth: false
    }
  },
  {
    path: '/manage-index',
    name: 'index',
    component: Layout,
    children: [
      // {
      //   desc: "测试页面",
      //   path: "test",
      //   component: Test,
      //   meta: {
      //     requireAuth: true
      //   }
      // },
      {
        desc: "课程备课",
        path: "course-prepare",
        component: ManageCourse,
        meta: {
          requireAuth: true
        }
      },
      {
        desc: "新增课件-主体部分",
        path: "courseware-wrapper",
        component: ManageCourseware,
        children: [
          // 默认路径渲染
          {
            desc: "卡片列表课件",
            name: "coursewareList",
            path: "",
            component: CoursewareCardList,
            meta: {
              requireAuth: true
            }
          },
          {
            desc: "新增课件",
            name: "addCourseware",
            path: "add-courseware",
            component: AddCourseware,
            meta: {
              requireAuth: true
            }
          }
        ]
      },
      {
        desc: "班级管理",
        path: "manage-class",
        // component: Test,
        meta: {
          requireAuth: true
        }
      },
    ]
  },
  {
    desc: "错误页面",
    path: '*',
    component: NotFound,
  }
];

// 创建路由对象
const router = new Router({
  mode: 'history',
  routes
});

// 配置路由拦截器
router.beforeEach((to, from, next) => {
  console.log("\n\n");
  console.log(to);
  console.log(to.matched);
  if (to.matched.length != 0) {
    console.log("「1」matched里面有元素");
    if (to.meta.requireAuth) {
      console.log("「2」需要登录权限 请判断token");
      if (getStore(TOKEN)) {
        console.log("「3」验证通过，爷这边请");
        next();
      } else {
        console.log("「3」验证失败，滚去登录吧你");
        next({ path: '/' })
      }
    } else {
      console.log("「2」无需登录权限 请判断是否在登录页面 验证token决定是否帮忙登录");
      if (to.path != "/" && to.path != "/login") {
        console.log("「3」不在登录页 走起 爱去哪去哪");
        next();
      } else {
        console.log("「3」在登录页 我看下你有没有token");
        if (getStore(TOKEN)) {
          console.log("「4」有了！！！送你去首页");
          next({
            path: "/manage-index/course-prepare"
          });
        } else {
          console.log("「4」没有！！！滚去登录");
          next();
        }
      }
    }
  } else {
    console.log("「1」matched里面没有元素 暂时我也不知道有什么用");
    // next({
    //   path: '/login',
    //   // 将跳转的路由path作为参数，登录成功后跳转到该路由
    //   query: { redirect: to.fullPath }
    // })
  }
})

// 导出路由信息
export default router;
